//
//  DBURLRoute.swift
//  DBCA
//
//  Created by mugua on 2024/7/6.
//

import Foundation

class DBURLRoute {
    
    static func checkUrl(url: String, toNativeFromVC: UIViewController) {
        checkUrl(url: url, replaceTop: false, toNativeFromVC: toNativeFromVC, animation: true)
    }
    
    static func travac(url: String) -> String {
        let abvac = "%20"
        var urlString = url
        if url.hasPrefix(abvac) {
            let startIndex = url.index(url.startIndex, offsetBy: abvac.count)
            urlString = String(url[startIndex..<url.endIndex])
            if urlString.hasPrefix(abvac) {
                return travac(url: urlString)
            }
        }
        return urlString
    }
    
    
    static func checkUrl(url: String, replaceTop: Bool, toNativeFromVC: UIViewController, animation: Bool) {
        
        guard let nav: UINavigationController = toNativeFromVC.navigationController else {
            return
        }
        
        var pushVC: UIViewController?
        var url = url.trimmingCharacters(in: NSCharacterSet.whitespacesAndNewlines)
        url = travac(url: url)
        let newUrl = NSURL(string: url)
        
        guard let path = newUrl?.path else { return  }
        
        if path.hasPrefix("/novaBlaze") {
            DBNavigator.currentNavigationController()?.tabBarController?.selectedIndex = 0
            return
        } else if path.hasPrefix("/pearlDrops") {
            
            DBNavigator.currentNavigationController()?.tabBarController?.selectedIndex = 1
            return
        } else if path.hasPrefix("/mapleLeaf") {
            
            DBNavigator.currentNavigationController()?.tabBarController?.selectedIndex = 2
            return
        } else if path.hasPrefix("/opalShimmer") {
            let loginVC = DBLoginViewController()
            let loginNav = DBBaseNavigationController(rootViewController: loginVC)
            loginNav.modalPresentationStyle = .fullScreen
            DBNavigator.currentViewController()?.present(loginNav, animated: true)
            return
            
        } else if path.hasPrefix("/quartzGleam") {
            
            let productDetailVC = DBProductDetailVC()
            
            pushVC = productDetailVC
            
        } else {
            
            if url.hasPrefix("http") || url.hasPrefix("https") {
                let webVc = DBBaseWebViewController()
                let parameters = URL(string: url)?.queryParameters
                var commonParams = DBNetworkTool.commonHeaderParameters()
                
                if let params = parameters {
                    for (key, value) in params {
                        commonParams[key] = value
                    }
                }
                
                if let urlString = url.components(separatedBy: "?").first {
                    let webUrl = DBNetworkTool.urlWithQueryParameters(baseURL: urlString, parameters: commonParams)
                    webVc.url = webUrl
                    nav.pushViewController(webVc, animated: animation)

                    if replaceTop {
                        var vcs = nav.viewControllers
                        vcs.removeAll(where: { $0 == toNativeFromVC })
                        nav.viewControllers = vcs
                    }
                }
            }
            
            return
        }
        
        if let pushVC = pushVC {
            nav.pushViewController(pushVC, animated: animation)
            if replaceTop {
                var vcs = nav.viewControllers
                vcs.removeAll(where: { $0 == toNativeFromVC })
                nav.viewControllers = vcs
            }
        }
    }
}
